package com.mrart.service.impl;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.mrart.entity.Authorization;
import com.mrart.entity.User;
import com.mrart.mysql.mapper.AuthorizationMapper;
import com.mrart.service.AppService;
import com.mrart.service.AuthorizationService;
import com.mrart.service.RoleService;
import com.mrart.service.UserService;

@Service
public class AuthorizationServiceImpl implements AuthorizationService {

    @Autowired
    private AuthorizationMapper authorizationMapper;
    @Autowired
    private UserService userService;
    @Autowired
    private RoleService roleService;
    @Autowired
    private AppService appService;
    
    /* (non-Javadoc)
     * @see com.mrart.service.impl.AuthorizationService#findOne(java.lang.Long)
     */
    @Override
    public Authorization findOne(Long authorizationId) {
        return authorizationMapper.selectByPrimaryKey(authorizationId);
    }
    
    /* (non-Javadoc)
     * @see com.mrart.service.impl.AuthorizationService#findAll()
     */
    @Override
    public List<Authorization> findAll() {
        return authorizationMapper.findAll();
    }
    
    /* (non-Javadoc)
     * @see com.mrart.service.impl.AuthorizationService#findByAppUser(java.util.Map)
     */
    @Override
    public Authorization findByAppUser(Map<String, Object> authorizationCondition) {
        List<Authorization> authorizations = authorizationMapper.findByAppUser(authorizationCondition);
        if(authorizations!=null && authorizations.size()>0) {
            return authorizations.get(0);
        } else {
            return null;
        }
    }
    
    /* (non-Javadoc)
     * @see com.mrart.service.impl.AuthorizationService#findRoles(java.lang.String, java.lang.String)
     */
    @Override
    @SuppressWarnings("unchecked")
    public Set<String> findRoles(String appKey, String username) {
        User user = userService.findByUsername(username);
        if(user == null) {
            return Collections.EMPTY_SET;
        }
        Long appId = appService.findAppIdByAppKey(appKey);
        if(appId == null) {
            return Collections.EMPTY_SET;
        }
        
        Map<String, Object> authorizationCondition = new HashMap<String, Object>();
        authorizationCondition.put("appId", appId);
        authorizationCondition.put("userId", user.getId());
        Authorization authorization = findByAppUser(authorizationCondition);
        if(authorization == null) {
            return Collections.EMPTY_SET;
        }
        return roleService.findRoles(authorization.getRoleIdList().toArray(new Long[0]));
    }
    
    /* (non-Javadoc)
     * @see com.mrart.service.impl.AuthorizationService#findPermissions(java.lang.String, java.lang.String)
     */
    @Override
    @SuppressWarnings("unchecked")
    public Set<String> findPermissions(String appKey, String username) {
        User user = userService.findByUsername(username);
        if(user == null) {
            return Collections.EMPTY_SET;
        }
        Long appId = appService.findAppIdByAppKey(appKey);
        if(appId == null) {
            return Collections.EMPTY_SET;
        }
        Map<String, Object> authorizationCondition = new HashMap<String, Object>();
        authorizationCondition.put("appId", appId);
        authorizationCondition.put("userId", user.getId());
        Authorization authorization = findByAppUser(authorizationCondition);
        if(authorization == null) {
            return Collections.EMPTY_SET;
        }
        return roleService.findPermissions(authorization.getRoleIdList().toArray(new Long[0]));
    }
}
